﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Eis51.CampanyCMS.Model.DownLoadMgnt.QueryO;
using Eis51.CampanyCMS.Business.DownLoadMgnt;
using System.Collections;
using Eis51.CampanyCMS.Model.DownLoadMgnt;
using System.Text;
using Eis51.CampanyCMS.Model.NewsManager;
using Eis51.CampanyCMS.Business.NewsManager;

namespace Eis51.Web.dataPage
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class HomeImg : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            StringBuilder sb = new StringBuilder();
            GetFirstImg(ref sb,context);
            context.Response.Write("[ " + sb.ToString() + "]");
        }

        public class NewsObj
        {
            public int Seqno { get; set; }
            public string Title { get; set; }
            public string ImgUrl { get; set; }
            public string Position { get; set; }
        }

        private void GetFirstImg(ref StringBuilder hoemData, HttpContext context)
        {
            DownloadFileQuery query = new DownloadFileQuery();
            //query.TypeId = ((int)common.common.FirstImg.Up).ToString();
            IList<DownloadFile> list = DownloadFileBLL.GetInstance().GetByCondition(query);

            if(list!=null && list.Count>0)
            {
                NewsObj model = null;
                string tmpStr = string.Empty; 
                string imgUrl = string.Empty;
                foreach (var et in list) {
                    if (string.IsNullOrEmpty(et.Title) || string.IsNullOrEmpty(et.FileLogo))
                    {
                        continue;
                    }
                    imgUrl ="/UpLoadFile/Img/"+ et.FileLogo.Replace("\\", "/");
                    //图片定向位置
                    DownloadFileType objPosition = DownloadFileTypeBLL.GetInstance().Get(new DownloadFileType.Key(int.Parse(et.ClassType)));

                    string tmpPosition = string.Empty;
                    if (int.Parse(et.ClassType) == (int)Eis51.Web.Admin.BasePage.IndexImgPosition.Customer)
                    {
                        Customer modelCustomer = CustomerBLL.GetInstance().Get(new Customer.Key(int.Parse(et.Code)));
                        if (null != modelCustomer)
                        {
                            tmpPosition = objPosition.Desc + "?pid=" + et.Code + "_" + modelCustomer.Type;
                        }
                    }
                    else
                    {
                        tmpPosition = objPosition.Desc + "?pid=" + et.Code;
                    }

                    model = new NewsObj
                    {
                        Seqno = et.DownloadFileId,
                        Title = et.Title,
                        ImgUrl = imgUrl,
                        Position = tmpPosition,
                    };
                    tmpStr = Eis51.Web.common.common.JavaScriptSerializerString(model);
                    hoemData.Append(",");
                    hoemData.Append(tmpStr);
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
